﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_TTStatistics : Form
    {
        private DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab m_bllFeeTTLoanAndFeeRegDetailInfoTab = new DGBY.BLL.FeeTTLoanAndFeeRegDetailInfoTab();
        UltraGridColumn m_ultraGridColumnBasicInfo = null;
        UltraGridColumn m_ultraGridColumnJiaoDan = null;
        UltraGridColumn m_ultraGridColumnShouKuan = null;
        UltraGridColumn m_ultraGridColumnKouFeeRecv = null;
        UltraGridColumn m_ultraGridColumnKouFeePay = null;
        public Frm_TTStatistics()
        {
            InitializeComponent();
        }

        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorWidth = 40;

        }

        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }

        /// 取得某月的第一天
        /// </summary>
        /// <param name="datetime">要取得月份第一天的时间</param>
        /// <returns></returns>
        private DateTime FirstDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day);
        }
        /// <summary>
        /// 取得某月的最后一天
        /// </summary>
        /// <param name="datetime">要取得月份最后一天的时间</param>
        /// <returns></returns>
        private DateTime LastDayOfMonth(DateTime datetime)
        {
            return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);
        }
        private void ProcessMultiHeader()
        {
            int nSpanX = -2;
            int nOriginX = 0;
            int nOriginXTemp = 0;
            bool bSpan = true;
            bool bStart = false;
            foreach (UltraGridColumn column2 in this.ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "交单次数")
                {
                    bSpan = false;
                    bStart = true;
                    //break;
                }
                if (str == "国内费用差额（RMB)")
                {
                    
                    bStart = false;
                    //break;
                }
                if (!bSpan)
                {
                    if (str == "基本资料")
                        continue;
                    if (bStart)
                    {
                        column2.RowLayoutColumnInfo.OriginY = 2;
                    }
                    else
                    {
                        column2.RowLayoutColumnInfo.SpanY = 4;
                        if (column2.Hidden == false)
                            nOriginXTemp += 2;
                    }

                }
                else
                {
                    nSpanX += 2;
                    column2.RowLayoutColumnInfo.OriginY = 2;
                }

            }

            m_ultraGridColumnBasicInfo = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("基本资料", "基本资料");
            m_ultraGridColumnBasicInfo.CellActivation = Activation.Disabled;
            m_ultraGridColumnBasicInfo.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnBasicInfo.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 4 * 2;
            m_ultraGridColumnJiaoDan = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("交单的情况", "交单的情况");
            m_ultraGridColumnJiaoDan.CellActivation = Activation.Disabled;
            m_ultraGridColumnJiaoDan.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnJiaoDan.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 4 * 2;
            m_ultraGridColumnShouKuan = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("收款的情况", "收款的情况");
            m_ultraGridColumnShouKuan.CellActivation = Activation.Disabled;
            m_ultraGridColumnShouKuan.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnShouKuan.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 3 * 2;
            m_ultraGridColumnKouFeeRecv = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("国内扣费(实收款)", "国内扣费(实收款)");
            m_ultraGridColumnKouFeeRecv.CellActivation = Activation.Disabled;
            m_ultraGridColumnKouFeeRecv.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnKouFeeRecv.RowLayoutColumnInfo.SpanY = 2;

            nOriginX = nOriginX + nSpanX;
            nSpanX = 2 * 2;
            m_ultraGridColumnKouFeePay = ultraGrid1.DisplayLayout.Bands[0].Columns.Add("国内扣费(实付款)", "国内扣费(实付款)");
            m_ultraGridColumnKouFeePay.CellActivation = Activation.Disabled;
            m_ultraGridColumnKouFeePay.SortIndicator = SortIndicator.Disabled;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.PreferredCellSize = new Size(1, 1);
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.AllowLabelSizing = RowLayoutSizing.None;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.LabelPosition = LabelPosition.LabelOnly;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.OriginX = nOriginX;//有2个列隐藏了
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.OriginY = 0;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.SpanX = nSpanX;
            m_ultraGridColumnKouFeePay.RowLayoutColumnInfo.SpanY = 2;
        }
        private void buttonImport_Click(object sender, EventArgs e)
        {
            ultraGrid1.DataSource = null;
            DateTime dtFirst = FirstDayOfMonth(this.dateTimePickerDate.Value);
            DateTime dtEnd = LastDayOfMonth(this.dateTimePickerDate.Value);
            DataSet ds = m_bllFeeTTLoanAndFeeRegDetailInfoTab.GetTTStatisticsInfoList(@"FeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate >= '" + dtFirst.Date + "'"
                                     + " and FeeTTLoanAndFeeRegDetailInfoTab.RecvMoneyDate <= '" + dtEnd.Date + "'");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ds.Tables[0].Columns.Add("国内费用差额（RMB)", System.Type.GetType("System.Decimal"));


                Decimal dtTotalRecvTemp = 0;
                Decimal dtTotalPayTemp = 0;
                Decimal dtTotalChaETemp = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["国内费用差额（RMB)"] = (Decimal)ds.Tables[0].Rows[i]["实收款合计"] - (Decimal)ds.Tables[0].Rows[i]["实付款合计"];
                    dtTotalRecvTemp += Convert.ToDecimal(ds.Tables[0].Rows[i]["实收款合计"].ToString());
                    dtTotalPayTemp += Convert.ToDecimal(ds.Tables[0].Rows[i]["实付款合计"].ToString());
                    dtTotalChaETemp += Convert.ToDecimal(ds.Tables[0].Rows[i]["国内费用差额（RMB)"].ToString());
                }
                DataRow objRow = ds.Tables[0].NewRow();
                objRow[1] = "合计";
                objRow["实收款合计"] = dtTotalRecvTemp;
                objRow["实付款合计"] = dtTotalPayTemp;
                objRow["国内费用差额（RMB)"] = dtTotalChaETemp;
                ds.Tables[0].Rows.Add(objRow);
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ProcessMultiHeader();
            }
        }

        private void Frm_TTStatistics_Load(object sender, EventArgs e)
        {
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);

            ultraGrid1.DisplayLayout.Bands[0].UseRowLayout = true;
            ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
                ugc.PerformAutoResize();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.Width++;
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }
    }
}
